<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        
        $this->load->model('luckydog/News_model', 'news');
	}

/**
 * 新闻列表
 * @return [type] [description]
 */
    public function index(){
        $type = $this->input->get("type",true) ? $this->input->get("type",true) : 1;
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $_SESSION['v_menu'] = 'news';
        $filter = array("type"=>$type);
        $limit = 20;
        $total_rows = $this->news->getNewsCnt($filter,$per_page,$limit);
        $base_url = '/luckydog/news/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);

        $newsList = $this->news->getNewsList($filter,$per_page, $limit);
        $data = array(
            'newsList'=>$newsList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            'type'=>$type
            );
        $this->load->view("/luckydog/news/index", $data);
    }

    public function textures()
    {
        $id = $this->input->get("id",true)?$this->input->get("id",true):"";
        $type = $this->input->get("type",true)?$this->input->get("type",true):0;
        if ($login_status === FALSE){
            header("Location:login");
        }
        $_SESSION['v_menu'] = 'news';
        $this->load->model('luckydog/newscategory_model', 'cat');
        $news_cat_list = $this->cat->get_cat_list();
        $data = ['news_cat_list'=>$news_cat_list];
        if(!empty($id)){
            $NewsInfo = $this->news->getNewsInfobyID($id);
            $data["NewsInfo"] = $NewsInfo;
        }
        $data['type'] = $type;
    	$this->load->view("/luckydog/news/textures", $data);
    }



    //编辑新闻
    public function editNews(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $cat_id = $this->input->post("cat_id",true) ? $this->input->post("cat_id",true) : 1;
        $content = $this->input->post("contents") ? $this->input->post("contents") : "";
        $title = $this->input->post("title",true) ? $this->input->post("title",true) : "";
        
        $data = array(
            "content"=>$content ,
            "title"=>$title ,
            "cat_id"=>$cat_id,
            "operationname"=>$_SESSION['adminUserInfo']['id'],
        );
        if(empty($id)){
            $data['addtime']=time();
        }
        $res = $this->news->editNews($id,$data);
        echo $res;
    }


    //编辑上线下，删除状态
    public function delNews(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $res =$this->news->delNews($id);
        echo $res;
    }

    public function upload_cover()
    {
        $this->load->library('Upload');
        $upload = new Upload();
        $file_name =$upload->execute($_FILES['Filedata']);

        $savepath='./upload/news/'.date('Y-m-d').'/';
        $upload->set_upload_dir($savepath);
        if (!file_exists($savepath)){
            mkdir($savepath);
        }
        if(is_string($file_name))
        {
            $res=json_encode(array('code'=>1,'type'=>0,'file_name'=>$file_name));

            echo $res;
        }
        else
        {
            echo json_encode(array('code'=>-1));
        }
    }

    //编辑上线下，删除状态
    public function editStatus(){
        $newsID = $this->input->post("newsID",true) ? $this->input->post("newsID",true) : 0;
        $onlinestatus = $this->input->post("onlinestatus",true) ? $this->input->post("onlinestatus",true) : "";
        $data = array(
            "newsID"=>$newsID,
            "onlinestatus"=>$onlinestatus,
        );
        $res =$this->news->editStatus($data);
        echo $res;
    }


}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/index.php */
